<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>queue.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_structs_queue.js.html">queue.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Datastructure: Queue.
<a name="line17"></a> *
<a name="line18"></a> *
<a name="line19"></a> * This file provides the implementation of a FIFO Queue structure.
<a name="line20"></a> * API is similar to that of com.google.common.collect.IntQueue
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.structs.Queue&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Class for FIFO Queue data structure.
<a name="line31"></a> *
<a name="line32"></a> * @constructor
<a name="line33"></a> */
<a name="line34"></a>goog.structs.Queue = function() {
<a name="line35"></a>  this.elements_ = [];
<a name="line36"></a>};
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * The index of the next element to be removed from the queue.
<a name="line41"></a> * @private
<a name="line42"></a> * @type {number}
<a name="line43"></a> */
<a name="line44"></a>goog.structs.Queue.prototype.head_ = 0;
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * The index at which the next element would be added to the queue.
<a name="line49"></a> * @private
<a name="line50"></a> * @type {number}
<a name="line51"></a> */
<a name="line52"></a>goog.structs.Queue.prototype.tail_ = 0;
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Puts the specified element on this queue.
<a name="line57"></a> * @param {*} element The element to be added to the queue.
<a name="line58"></a> */
<a name="line59"></a>goog.structs.Queue.prototype.enqueue = function(element) {
<a name="line60"></a>  this.elements_[this.tail_++] = element;
<a name="line61"></a>};
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * Retrieves and removes the head of this queue.
<a name="line66"></a> * @return {*} The element at the head of this queue. Returns undefined if the
<a name="line67"></a> *     queue is empty.
<a name="line68"></a> */
<a name="line69"></a>goog.structs.Queue.prototype.dequeue = function() {
<a name="line70"></a>  if (this.head_ == this.tail_) {
<a name="line71"></a>    return undefined;
<a name="line72"></a>  }
<a name="line73"></a>  var result = this.elements_[this.head_];
<a name="line74"></a>  delete this.elements_[this.head_];
<a name="line75"></a>  this.head_++;
<a name="line76"></a>  return result;
<a name="line77"></a>};
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Retrieves but does not remove the head of this queue.
<a name="line82"></a> * @return {*} The element at the head of this queue. Returns undefined if the
<a name="line83"></a> *     queue is empty.
<a name="line84"></a> */
<a name="line85"></a>goog.structs.Queue.prototype.peek = function() {
<a name="line86"></a>  if (this.head_ == this.tail_) {
<a name="line87"></a>    return undefined;
<a name="line88"></a>  }
<a name="line89"></a>  return this.elements_[this.head_];
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Returns the number of elements in this queue.
<a name="line95"></a> * @return {number} The number of elements in this queue.
<a name="line96"></a> */
<a name="line97"></a>goog.structs.Queue.prototype.getCount = function() {
<a name="line98"></a>  return this.tail_ - this.head_;
<a name="line99"></a>};
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Returns true if this queue contains no elements.
<a name="line104"></a> * @return {boolean} true if this queue contains no elements.
<a name="line105"></a> */
<a name="line106"></a>goog.structs.Queue.prototype.isEmpty = function() {
<a name="line107"></a>  return this.tail_ - this.head_ == 0;
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Removes all elements from the queue.
<a name="line113"></a> */
<a name="line114"></a>goog.structs.Queue.prototype.clear = function() {
<a name="line115"></a>  this.elements_.length = 0;
<a name="line116"></a>  this.head_ = 0;
<a name="line117"></a>  this.tail_ = 0;
<a name="line118"></a>};
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Returns true if the given value is in the queue.
<a name="line123"></a> * @param {*} obj The value to look for.
<a name="line124"></a> * @return {boolean} Whether the object is in the queue.
<a name="line125"></a> */
<a name="line126"></a>goog.structs.Queue.prototype.contains = function(obj) {
<a name="line127"></a>  return goog.array.contains(this.elements_, obj);
<a name="line128"></a>};
<a name="line129"></a>
<a name="line130"></a>
<a name="line131"></a>/**
<a name="line132"></a> * Removes the first occurrence of a particular value from the queue.
<a name="line133"></a> * @param {*} obj Object to remove.
<a name="line134"></a> * @return {boolean} True if an element was removed.
<a name="line135"></a> */
<a name="line136"></a>goog.structs.Queue.prototype.remove = function(obj) {
<a name="line137"></a>  var index = goog.array.indexOf(this.elements_, obj);
<a name="line138"></a>  if (index &lt; 0) {
<a name="line139"></a>    return false;
<a name="line140"></a>  }
<a name="line141"></a>  if (index == this.head_) {
<a name="line142"></a>    this.dequeue();
<a name="line143"></a>  } else {
<a name="line144"></a>    goog.array.removeAt(this.elements_, index);
<a name="line145"></a>    this.tail_--;
<a name="line146"></a>  }
<a name="line147"></a>  return true;
<a name="line148"></a>};
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * Returns all the values in the queue.
<a name="line153"></a> * @return {Array} An array of the values in the queue.
<a name="line154"></a> */
<a name="line155"></a>goog.structs.Queue.prototype.getValues = function() {
<a name="line156"></a>  return this.elements_.slice(this.head_, this.tail_);
<a name="line157"></a>};
</pre>


</body>
</html>
